/*
 * OREKIT-X
 * Copyright 2002-2008 CS Communication & Systemes
 * 
 * Parts of this software package have been licensed to CS
 * Communication & Systemes (CS) under one or more contributor license
 * agreements.  See the NOTICE file distributed with this work for
 * additional information.
 *  
 * This is an experimental copy of OREKIT from www.orekit.org.
 * Please use the original OREKIT from orekit.org for normal work
 * unrelated to this research project.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License.  You
 * may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.  See the License for the specific language governing
 * permissions and limitations under the License.
 */
package ore.data;

/** 
 * Term in a nutation series 
 * 
 * @see PoissonSeries
 * 
 * @see PlanetaryTerm
 * @see LuniSolarTerm
 * @see NoFarPlanetsTerm
 * @see GeneralTerm
 * 
 * @author Luc Maisonobe
 */
public abstract class SeriesTerm
    implements java.io.Serializable 
{
    /** 
     * Coefficient for the sine of the argument.
     */
    private final double sinCoeff;
    /** 
     * Coefficient for the cosine of the argument.
     */
    private final double cosCoeff;


    /** 
     * @param sinCoeff Coefficient for the sine of the argument
     * @param cosCoeff Coefficient for the cosine of the argument
     */
    protected SeriesTerm(double sinCoeff, double cosCoeff) {
        super();
        this.sinCoeff = sinCoeff;
        this.cosCoeff = cosCoeff;
    }


    /** 
     * Compute the value of the term for the current date.
     * @param elements luni-solar and planetary elements for the current date
     * @return Current value of the term
     */
    public double value(BodiesElements elements) {
        double a = this.argument(elements);
        return (this.sinCoeff * Math.sin(a)) + (this.cosCoeff * Math.cos(a));
    }
    /** 
     * Compute the argument for the current date.
     * @param elements luni-solar and planetary elements for the current date
     * @return Current value of the argument
     */
    protected abstract double argument(BodiesElements elements);

    /** 
     * Called from {@link PoissonSeries} to check null coefficients
     * and return a term of an appropriate type to avoid too many
     * unnecessary multiplications by zero coefficients.
     * 
     * @param sinCoeff coefficient for the sine of the argument
     * @param cosCoeff coefficient for the cosine of the argument
     * @param cL coefficient for mean anomaly of the Moon
     * @param cLPrime coefficient for mean anomaly of the Sun
     * @param cF coefficient for L - &Omega; where L is the mean longitude of the Moon
     * @param cD coefficient for mean elongation of the Moon from the Sun
     * @param cOmega coefficient for mean longitude of the ascending node of the Moon
     * @param cMe coefficient for mean Mercury longitude
     * @param cVe coefficient for mean Venus longitude
     * @param cE coefficient for mean Earth longitude
     * @param cMa coefficient for mean Mars longitude
     * @param cJu coefficient for mean Jupiter longitude
     * @param cSa coefficient for mean Saturn longitude
     * @param cUr coefficient for mean Uranus longitude
     * @param cNe coefficient for mean Neptune longitude
     * @param cPa coefficient for general accumulated precession in longitude
     * 
     * @return A nutation serie term instance well suited for the set of coefficients
     */
    public static SeriesTerm TermFor(double sinCoeff, double cosCoeff,
                                     int cL, int cLPrime, int cF,
                                     int cD, int cOmega,
                                     int cMe, int cVe, int cE,
                                     int cMa, int cJu, int cSa,
                                     int cUr, int cNe, int cPa)
    {
        if (cL == 0 && cLPrime == 0 && cF == 0 && cD == 0 && cOmega == 0)
        {
            return new PlanetaryTerm(sinCoeff, cosCoeff,
                                     cMe, cVe, cE, cMa, cJu, cSa, cUr, cNe, cPa);

        }
        else if (cMe == 0 && cVe == 0 && cE == 0 && cMa == 0 && cJu == 0 &&
                 cSa == 0 && cUr == 0 && cNe == 0 && cPa == 0)
        {
            return new LuniSolarTerm(sinCoeff, cosCoeff,
                                     cL, cLPrime, cF, cD, cOmega);

        }
        else if (cLPrime == 0 && cUr == 0 && cNe == 0 && cPa == 0)
        {
            return new NoFarPlanetsTerm(sinCoeff, cosCoeff,
                                        cL, cF, cD, cOmega,
                                        cMe, cVe, cE, cMa, cJu, cSa);
        }
        else {
            return new GeneralTerm(sinCoeff, cosCoeff,
                                   cL, cLPrime, cF, cD, cOmega,
                                   cMe, cVe, cE, cMa, cJu, cSa, cUr, cNe, cPa);
        }
    }
}
